<?php

namespace App\Api;

use App\Domain\Examples\ViewUser;
use PhalApi\Api;
use App\Domain\Examples\City as DomainCURD;
use App\Domain\Examples\SpotType as SpotTypeDAO;
use App\Domain\Examples\SpotView as SpotViewDAO;
use App\Domain\Examples\ViewUser as ViewUserDAO;
use App\Domain\Examples\HotViewKV as HotViewKVDAO;
use App\Domain\Examples\ArticleList as ArticleListDAO;
use App\Domain\Examples\ArticleType as ArticleTypeDAO;



/**
 *☛☛接口请点这里☚☚
 * @未完成的功能
 * 3.我的预约->相关操作（增 删 查 详情） 预约管理 票务管理
 * 2.关于我们，联系我们等文章内容 √
 *
 */
class Hello extends Api
{
    //控制输入参数
    public function getRules()
    {
        return array(
            //方法名
            'login' => array(//name-传过来的参数
                'name' => array('name' => 'name', 'require' => true, 'min' => 'admin', 'desc' => 'name'),
                'password' => array('name' => 'password', 'require' => true, 'min' => 'admin', 'desc' => 'password'),
            ),
            'getAllCitysByWord' => array(//name-传过来的参数
                'word' => array('name' => 'word', 'require' => true, 'min' => 'admin', 'desc' => '模糊查询城市名称')
            ),
            'getMain' => array(//name-传过来的参数
                'cityid' => array('name' => 'cityid', 'require' => true, 'min' => '1', 'desc' => '当前城市的id'),
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '第几页'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'min' => 1, 'max' => 20, 'default' => 10, 'desc' => '分页数量'),
            ),
            'getSpotViewById' => array(//name-传过来的参数
                'id' => array('name' => 'id', 'require' => true, 'min' => 'admin', 'desc' => '景区id')
            ),
            'getSpotViewByWord' => array(//name-传过来的参数
                'word' => array('name' => 'word', 'desc' => '景区搜索关键词'),
                'sort_type_id' => array('name' => 'sort_type_id',   'desc' => '景区分类ID'),
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '第几页'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'min' => 1, 'max' => 20, 'default' => 10, 'desc' => '分页数量'),
            ),
            'getLoginStateByOpenId' => array(//name-传过来的参数
                'id' => array('name' => 'id', 'require' => true, 'min' => 'admin', 'desc' => '当前登录的openid')
            ),
            'getUserDetailsByOpenId' => array(//name-传过来的参数
                'id' => array('name' => 'id', 'require' => true, 'min' => 'admin', 'desc' => '当前账户的openId')
            ),
            'getArticleDetailByName' => array(//name-传过来的参数
                'name' => array('name' => 'name', 'require' => true, 'min' => 'admin', 'desc' => '关于我们')
            ));

    }
    /**
     *景点相关--根据关键词获得景点列表
     * @desc 根据关键词模糊查询景点列表
     */
    public function getSpotViewByWord()
    {
        $spotView = new SpotViewDAO();
        return $spotView->getSpotViewByWord($this->word,$this->sort_type_id,$this->page,$this->perpage);
    }
    /**
    *输入关于我们 查询出详情
     * @desc 显示“关于我们”等文章内容
     */
    public function getArticleDetailByName(){
        $name=$this->name;
        $model=new ArticleListDAO();
        $type=new ArticleTypeDAO();
        $id=$type->getTypeIDByName($name);
        $students= json_encode($id,true);//得到的是 array
        $student= json_decode($students, true);//得到的是 array
        if(count($student)>0){
            $context=$model->getDetailByTypeID($student[0]['id']);
            return $context;
        }else{
            return null;
        }
    }
    /**
     *城市列表--获得城市列表
     * @desc 无传入参数
     */
    public function getCitys()
    {
        $rs = array();
        $domain = new DomainCURD();
        $citys = array();
        $hots_city = array();
        $hots_city['title'] = '热门城市';
        $hots_city['type'] = 'hot';
        $hots_city['item'] = $domain->getHotCitys();
        $citys[] = $hots_city;

        foreach (range('a', 'z') as $v) {
            $cc = $domain->getCitysByPingName($v);
            if (count($cc) > 0) {
                $citys[] = array('title' => $v, 'item' => $domain->getCitysByPingName($v));
            }
        }
        $rs[] = $citys;
        return $citys;
    }

    /**
     *城市列表--根据城市名称模糊查询
     * @desc 根据输入的城市名称进行模糊查询
     */
    public function getAllCitysByWord()
    {
        $domain = new DomainCURD();
        $word = $this->word;//当前查询的城市名字
        return $domain->getAllCitysByWord($word);
    }

    /**
     *首页--获得首页数据
     * @desc 根据城市ID获得首页数据
     */
    public function getMain()
    {
        $spotType = new SpotTypeDAO();
        $spotView = new SpotViewDAO();
        $id = $this->cityid;//获得当前城市的id
//        $id='130602';
        $rs = array();
        $index = $this->page;//当前页码
        if ($index == 1) {
            $rs["alltag"] = $spotType->getSpotTypeByID($id);
        }
        $page_size = $this->perpage;//当前每页个数
        if (empty($page_size)) {
            $page_size = 10;
        }
        $rs["alllist"] = $spotView->getList($this->page, $page_size,$id);
        return $rs;
    }

    /**
     *景点相关--根据id获得景点详情
     * @desc 根据景点ID获得详情
     */
    public function getSpotViewById()
    {
        $spotView = new SpotViewDAO();
        return $spotView->get($this->id);
    }



    /**
     *用户相关--获得当前账户信息
     * @desc 通过Id获得账户详情
     */
    public function getUserDetailsByOpenId()
    {
        $spotView = new ViewUserDAO();
        return $spotView->get($this->id);
    }

    /**
     *用户相关--根据openId获得当前账户登录状态
     * @desc 通过openId获得登录状态
     * @return string success 登录成功
     */
    public function getLoginStateByOpenId()
    {
        $spotView = new ViewUserDAO();
        $detail = $spotView->getLoginStateByOpenId($this->id);
        return array('success' => count($detail) > 0);
    }
    /**
     * 热门景点
     * @desc 热门关键词列表
     * @return  热门关键词列表
     */
    public function getHotKeyWordByWord()
    {
        $hotview=new HotViewKVDAO();
        return $hotview->getHotKeyWordByWord();
    }

}